package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.RegisterException;
import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.MultipartFileUtils;
import com.fantastic.mapper.zjs.AccountCouponsMapper;
import com.fantastic.mapper.zjs.AccountMapper;
import com.fantastic.mapper.zjs.UserMapper;
import com.fantastic.model.pojo.zjs.Account;
import com.fantastic.model.pojo.zjs.AccountCoupons;
import com.fantastic.model.pojo.zjs.User;
import com.fantastic.model.vo.zjs.AccountCouponsVo;
import com.fantastic.model.vo.zjs.UserVo;
import com.fantastic.service.zjs.UserService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/5/22 20:30
 * @description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private AccountMapper accountMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private AccountCouponsMapper accountCouponsMapper;

    /**
     * 查询
     * 无条件时是查询全部的分页查询
     * 有条件时是查询条件的分页查询
     *
     * @return
     */
    @Override
    public List<User> listUsers(User user) {
        //mybatis的分页插件拦截器，必须先new出实例或是直接返回数据中进行拦截分页。
        List<User> users = userMapper.listUsers(user);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), users);
        for (User list : users) {
            list.setUserPicture(FilesUtils.getSlashParse(list.getUserPicture()));
        }
        return users;
    }

    /**
     * 查询单个用户
     *
     * @param userId
     * @return
     */
    public User getUser(Integer userId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), userId);
        User user = userMapper.getUser(userId);
        user.setUserPicture(FilesUtils.getSlashParse(user.getUserPicture()));
        return user;
    }

    /**
     * 查询UserVo信息
     *
     * @param account
     * @return
     */
    @Override
    public UserVo getUserVo(Account account) {
        UserVo userVo = new UserVo();
        //通过手机号拿到完整的账号信息
        Account getAccount = accountMapper.getAccount(account.getAccountId());
        //设置用户基本信息
        userVo.setUser(userMapper.getUser(getAccount.getUserId()));


        //设置用户手机号和余额
        userVo.setPhone(getAccount.getPhone());
        userVo.setAccountStored(getAccount.getAccountStored());
        userVo.setAccountId(getAccount.getAccountId());
        //设置用户所持卷数量
        AccountCoupons accountCoupons = new AccountCoupons();
        accountCoupons.setAccountId(getAccount.getAccountId());

        AccountCouponsVo accountCouponsList = accountCouponsMapper.getAccountCoupons(getAccount.getAccountId());

        if (accountCouponsList.getCoupons().size() == 0) {
            userVo.setCouponsCount(0);
        } else {
            userVo.setCouponsCount(accountCouponsList.getCoupons().size());
        }

        userVo.getUser().setUserPicture(FilesUtils.getSlashParse(userVo.getUser().getUserPicture()));
        return userVo;
    }

    /**
     * 添加用户信息
     * 注册账号时调用。
     *
     * @param user
     */
    @Override
    public Integer insertUser(User user) {
        //判断用户信息是否为空！
        userIsNull(user);
        //获取执行添加sql语句的受影响行数
//        Integer uId = userMapper.insertUser(user);
        //执行添加用户信息
        userMapper.insertUser(user);
        if (user.getUserId() != null && user.getUserId() > 0) {
            //拿到自增的主键
            return user.getUserId();
        }
        //抛出注册时服务器异常(无数据状态码)
        throw new RegisterException(RespStatusInfo.REGIS_SERVER_ERROR);
    }

    /**
     * 判断用户信息是否有为空
     *
     * @param user
     */
    public void userIsNull(User user) {
        if (user.getUserName() != null && user.getUserName() != "" && user.getUserIntegral() != null && user.getUserPicture() != null && user.getUserPicture() != "") {
            return;
        }
        //抛出用户信息信息不完整异常
        throw new RegisterException(RespStatusInfo.INSERT_ERROR);
    }


    /**
     * 修改用户信息   修改积分也可以使用该请求，修改头像
     *
     * @param file 头像文件
     * @param user 用户信息对象
     */
    @Override
    public Integer updateUser(MultipartFile file, User user) {
        //查询要修改的这个用户有没有头像
        User userUpdate = getUser(user.getUserId());
        //文件不为空，表示有文件，用户要修改头像
        if (file != null && file.getSize() != 0) {
            //判断文件类型是否支持        传入supportType()中判断是否支持
            MultipartFileUtils.supportType(file.getOriginalFilename());
            //将文件写入到本地，返回文件相对路径
            String relativePath = MultipartFileUtils.writeFile(file, "头像");
            //添加到user对象中
            user.setUserPicture(relativePath);
        }
        //数据库的头像路径不为空并且file不为空，对原来的文件进行删除处理
        if (userUpdate.getUserPicture() != null && file != null && file.getSize() != 0) {
            //对头像进行删除
            MultipartFileUtils.deleteFile(userUpdate.getUserPicture());
        }

        return userMapper.updateUser(user);
    }

    /**
     * 删除用户信息
     * 要删除用户信息，要先删除账号信息，因为账号表外键了这个用户信息表
     *
     * @param uId
     */
    @Override
    public Integer deleteUser(Integer uId) {
        return userMapper.deleteUser(uId);
    }

}
